package com.jtech.together.util;

import android.support.annotation.NonNull;

import com.jtech.together.manager.UserManager;
import com.jtech.together.model.AddFriendMessage;
import com.jtech.together.model.AgreeAddFriendMessage;

import java.util.List;
import java.util.Map;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMAudioMessage;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMImageMessage;
import cn.bmob.newim.bean.BmobIMLocationMessage;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMMessageType;
import cn.bmob.newim.bean.BmobIMTextMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.core.BmobIMClient;
import cn.bmob.newim.listener.ConversationListener;
import cn.bmob.newim.listener.MessageSendListener;

/**
 * 即时通讯通用方法
 * Created by wuxubaiyang on 2017/5/13.
 */

public class IM {
    private BmobIMConversation conversation;

    /**
     * 主构造
     *
     * @param conversation
     */
    public IM(@NonNull BmobIMConversation conversation) {
        //创建一个管理消息发送的会话
        this.conversation = BmobIMConversation
                .obtain(BmobIMClient.getInstance(), conversation);
    }

    /**
     * 构造一个已存在的会话
     *
     * @param conversation
     * @return
     */
    public static IM build(@NonNull BmobIMConversation conversation) {
        return new IM(conversation);
    }

    /**
     * 构造一个会话
     *
     * @param userInfo
     * @param isTransient
     * @param conversationListener
     * @return
     */
    public static IM build(@NonNull BmobIMUserInfo userInfo, boolean isTransient, ConversationListener conversationListener) {
        BmobIMConversation conversation = BmobIM
                .getInstance()
                .startPrivateConversation(userInfo, isTransient, conversationListener);
        return null != conversation ? new IM(conversation) : null;
    }

    /**
     * 构造一个常态会话
     *
     * @param userInfo
     * @param conversationListener
     * @return
     */
    public static IM build(@NonNull BmobIMUserInfo userInfo, ConversationListener conversationListener) {
        return build(userInfo, false, conversationListener);
    }

    /**
     * 构造一个暂态会话
     *
     * @param userInfo
     * @param conversationListener
     * @return
     */
    public static IM buildTransient(@NonNull BmobIMUserInfo userInfo, ConversationListener conversationListener) {
        return build(userInfo, true, conversationListener);
    }

    /**
     * 根据目标用户id获取对象的会话
     *
     * @param targetId
     * @return
     */
    public static BmobIMConversation toConversation(String targetId) {
        List<BmobIMConversation> conversations = BmobIM.getInstance().loadAllConversation();
        if (null != conversations) {
            for (int i = 0; i < conversations.size(); i++) {
                List<BmobIMMessage> imMessages = conversations.get(i).getMessages();
                if (null != imMessages) {
                    BmobIMMessage imMessage = imMessages.get(0);
                    if (targetId.equals(imMessage.getFromId()) ||
                            targetId.equals(imMessage.getToId())) {
                        return conversations.get(i);
                    }
                } else {
                    //如果会话没有消息则删除会话
                    BmobIM.getInstance().deleteConversation(conversations.get(i));
                }
            }
        }
        IM im = build(UserManager.getIMUserInfo(targetId), null);
        return null != im ? im.getConversation() : null;
    }

    /**
     * 获取消息内容
     *
     * @param imMessage
     * @return
     */
    public static String getMessageContent(BmobIMMessage imMessage) {
        if (BmobIMMessageType.IMAGE.getType().equals(imMessage.getMsgType())) {
            return "[图片]";
        } else if (BmobIMMessageType.VOICE.getType().equals(imMessage.getMsgType())) {
            return "[音频]";
        } else if (BmobIMMessageType.LOCATION.getType().equals(imMessage.getMsgType())) {
            return "[位置]";
        } else {
            return imMessage.getContent();
        }
    }

    /**
     * 获取当前会话
     *
     * @return
     */
    public BmobIMConversation getConversation() {
        return conversation;
    }

    /**
     * 发送文本消息
     *
     * @param text
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendTextMessage(String text, MessageSendListener messageSendListener) {
        BmobIMTextMessage textMessage = new BmobIMTextMessage();
        textMessage.setContent(text);
        //设置会话最后一条消息
        conversation.setDraft(text);
        //发送消息
        return sendMessage(textMessage, messageSendListener);
    }

    /**
     * 发送文本消息
     *
     * @param text
     * @param extraMap
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendTextMessage(String text, Map<String, Object> extraMap, MessageSendListener messageSendListener) {
        BmobIMTextMessage textMessage = new BmobIMTextMessage();
        textMessage.setContent(text);
        textMessage.setExtraMap(extraMap);
        //设置会话最后一条消息
        conversation.setDraft(text);
        //发送消息
        return sendMessage(textMessage, messageSendListener);
    }

    /**
     * 发送图片消息
     *
     * @param localPath
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendImageMessage(String localPath, MessageSendListener messageSendListener) {
        BmobIMImageMessage imageMessage = new BmobIMImageMessage(localPath);
        //发送消息
        return sendMessage(imageMessage, messageSendListener);
    }

    /**
     * 发送图片消息
     *
     * @param localPath
     * @param extraMap
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendImageMessage(String localPath, Map<String, Object> extraMap, MessageSendListener messageSendListener) {
        BmobIMImageMessage imageMessage = new BmobIMImageMessage(localPath);
        imageMessage.setExtraMap(extraMap);
        //发送消息
        return sendMessage(imageMessage, messageSendListener);
    }

    /**
     * 发送网络图片消息
     *
     * @param remoteUrl
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendRemoteImageMessage(String remoteUrl, MessageSendListener messageSendListener) {
        BmobIMImageMessage imageMessage = new BmobIMImageMessage();
        imageMessage.setRemoteUrl(remoteUrl);
        //发送消息
        return sendMessage(imageMessage, messageSendListener);
    }

    /**
     * 发送网络图片消息
     *
     * @param remoteUrl
     * @param extraMap
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendRemoteImageMessage(String remoteUrl, Map<String, Object> extraMap, MessageSendListener messageSendListener) {
        BmobIMImageMessage imageMessage = new BmobIMImageMessage();
        imageMessage.setRemoteUrl(remoteUrl);
        imageMessage.setExtraMap(extraMap);
        //发送消息
        return sendMessage(imageMessage, messageSendListener);
    }

    /**
     * 发送语音消息
     *
     * @param localPath
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendAudioMessage(String localPath, MessageSendListener messageSendListener) {
        BmobIMAudioMessage audioMessage = new BmobIMAudioMessage(localPath);
        //发送消息
        return sendMessage(audioMessage, messageSendListener);
    }

    /**
     * 发送语音消息
     *
     * @param localPath
     * @param extraMap
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendAudioMessage(String localPath, Map<String, Object> extraMap, MessageSendListener messageSendListener) {
        BmobIMAudioMessage audioMessage = new BmobIMAudioMessage(localPath);
        audioMessage.setExtraMap(extraMap);
        //发送消息
        return sendMessage(audioMessage, messageSendListener);
    }

    /**
     * 发送网络语音消息
     *
     * @param remoteUrl
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendRemoteAudioMessage(String remoteUrl, MessageSendListener messageSendListener) {
        BmobIMAudioMessage audioMessage = new BmobIMAudioMessage();
        audioMessage.setRemoteUrl(remoteUrl);
        //发送消息
        return sendMessage(audioMessage, messageSendListener);
    }

    /**
     * 发送网络语音消息
     *
     * @param remoteUrl
     * @param extraMap
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendRemoteAudioMessage(String remoteUrl, Map<String, Object> extraMap, MessageSendListener messageSendListener) {
        BmobIMAudioMessage audioMessage = new BmobIMAudioMessage();
        audioMessage.setRemoteUrl(remoteUrl);
        audioMessage.setExtraMap(extraMap);
        //发送消息
        return sendMessage(audioMessage, messageSendListener);
    }

    /**
     * 发送位置消息
     *
     * @param address
     * @param latitude
     * @param longitude
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendLocationMessage(double longitude, double latitude, String address,
                                             MessageSendListener messageSendListener) {
        BmobIMLocationMessage locationMessage = new BmobIMLocationMessage(address, latitude, longitude);
        //发送消息
        return sendMessage(locationMessage, messageSendListener);
    }

    /**
     * 发送位置消息
     *
     * @param address
     * @param latitude
     * @param longitude
     * @param extraMap
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendLocationMessage(String address, double latitude, double longitude,
                                             Map<String, Object> extraMap, MessageSendListener messageSendListener) {
        BmobIMLocationMessage locationMessage = new BmobIMLocationMessage(address, latitude, longitude);
        locationMessage.setExtraMap(extraMap);
        //发送消息
        return sendMessage(locationMessage, messageSendListener);
    }

    /**
     * 发送邀请消息
     *
     * @param content
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage sendAddFriendMessage(String content, MessageSendListener messageSendListener) {
        AddFriendMessage addFriendMessage = new AddFriendMessage();
        addFriendMessage.setContent(content);
        //设置会话最后一条消息
        conversation.setDraft(AddFriendMessage.MESSAGE_TYPE_ADD_FRIEND);
        //发送消息
        return sendMessage(addFriendMessage, messageSendListener);
    }

    /**
     * 同意邀请消息
     *
     * @param content
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage agreeAddFriendMessage(String content, MessageSendListener messageSendListener) {
        AgreeAddFriendMessage agreeAddFriendMessage = new AgreeAddFriendMessage();
        agreeAddFriendMessage.setContent(content);
        //设置会话最后一条消息
        conversation.setDraft(content);
        //发送消息
        return sendMessage(agreeAddFriendMessage, messageSendListener);
    }

    /**
     * 发送消息
     *
     * @param imMessage
     * @return
     */
    public BmobIMMessage sendMessage(@NonNull BmobIMMessage imMessage, MessageSendListener messageSendListener) {
        if (null != conversation) {
            if (null == messageSendListener) {
                conversation.sendMessage(imMessage);
            } else {
                conversation.sendMessage(imMessage, messageSendListener);
            }
        }
        return imMessage;
    }

    /**
     * 重发消息
     *
     * @param imMessage
     * @param messageSendListener
     * @return
     */
    public BmobIMMessage reSendMessage(@NonNull BmobIMMessage imMessage, @NonNull MessageSendListener messageSendListener) {
        if (null != conversation) {
            conversation.resendMessage(imMessage, messageSendListener);
        }
        return imMessage;
    }
}